﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StatusBar : MonoBehaviour
{
    public static StatusBar Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<StatusBar>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("StatusBar");
                _Instance = obj.AddComponent<StatusBar>();
            }
            return _Instance;
        }
        set
        {

        }
    }
    private static StatusBar _Instance;

    private Ray ray;
    private RaycastHit2D[] hit;
    private GameObject obj;
    private BattleObject battleObj;
    private HitPoint hp;

    //UI模块
    public Text objName; //姓名
    public Image blood;//血量条
    public Text hpText; //血量值
    public Text damageText; //伤害值
    public Text rangeText; //攻击范围
    public Text speedText; //攻击速度
    public Text typeText; //攻击类型
    public LayoutGroup buffStatus;//BUFF栏
    public Text wave;//波次栏
    public Text moveSpeedText;//移动速度
    public Image character;//人物头像

    private GameObject target;
    private GameObject player;
    private List<BuffImage> buffImageList;
    private List<BuffInfo> buffInfoList;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("Player");
        obj = player;
        battleObj = obj.GetComponent<BattleObject>();
        hp = obj.GetComponent<HitPoint>();
        objName.text = battleObj.battleObjectName;
        if (hp != null)
        {
            blood.fillAmount = hp.getCurrentHP() / hp.getMaxHP();
            hpText.text = System.Math.Round(hp.getCurrentHP()) + "/" + System.Math.Round(hp.getMaxHP());
        }
        damageText.text = battleObj.damage.ToString() + "Hp";
        rangeText.text = System.Math.Round(battleObj.searchRange / ConstOfMap.xGap, 1).ToString() + "格";
        speedText.text = System.Math.Round(battleObj.coolTime + battleObj.frontActionTime + battleObj.attackActionTime + battleObj.afterActionTime, 1).ToString() + "s/次";
        //typeText.text = AttackTypeToChinese(battleObj.attackType);
        buffImageList = new List<BuffImage>();
        target = player;
    }

    // Update is called once per frame
    void Update()
    {
        if (player == null)
            return;
        /*if (Input.GetMouseButtonDown(0))
        {
            bool hitOn = false;
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            hit = Physics2D.RaycastAll(new Vector2(ray.origin.x, ray.origin.y), Vector2.zero);
            if (hit != null)
            {
                for (int i = 0; i < hit.Length; i++)
                {
                    if (hit[i].collider.gameObject.GetComponent<BattleObject>() != null && hit[i].collider.gameObject.GetComponent<HitPoint>() != null)
                    {
                        hitOn = true;
                        obj = hit[i].collider.gameObject;
                        target = obj;
                        UpdateStatusBar();
                        break;
                    }
                }
            }
            if (!hitOn)
            {
                target = player;
                UpdateStatusBar();
            }
        }*/

        if (target != null)
        {
            UpdateHPBar();
            BodyColor outline = target.GetComponent<BodyColor>();
            if (outline != null)
            {
                outline.wantShow = true;
            }
        }
        else
        {
            target = player;
            UpdateStatusBar();
        }
        if (target != player)
            ViewableRange.Instance.ChangeTarget(target);
        else
            ViewableRange.Instance.ChangeTarget(null);
        UpdateBuff();
        UpdateStatusBar();
    }

    private void UpdateStatusBar()
    {
        battleObj = target.GetComponent<BattleObject>();
        hp = target.GetComponent<HitPoint>();
        objName.text = battleObj.battleObjectName;
        wave.text = "波次: "+APController.Instance.wave;
        damageText.text = battleObj.damage.ToString() + "Hp";
        rangeText.text = System.Math.Round(battleObj.searchRange / ConstOfMap.xGap, 1).ToString() + "格";
        speedText.text = System.Math.Round(battleObj.coolTime + battleObj.frontActionTime + battleObj.attackActionTime + battleObj.afterActionTime, 1).ToString() + "秒/次";
        //typeText.text = AttackTypeToChinese(battleObj.attackType);
        if(target==player)
        {
            moveSpeedText.text = System.Math.Round(player.GetComponent<Player>().speed,1)+"格/s";
        }
        else if(target.GetComponent<Move>()!=null)
        {
            moveSpeedText.text= System.Math.Round(target.GetComponent<Move>().speed,1)+"格/s";
        }
        else
        {
            moveSpeedText.text = "0格/s";
        }
        UpdateHPBar();
        string str = target.gameObject.name;
        str = str.Replace("(Clone)", "");

        Texture2D tex = Resources.Load("CharaterImage/" + str) as Texture2D;

        if (tex != null)
            character.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
        //else
            //Debug.Log("null " + str);
    }

    private void UpdateHPBar()
    {
        if (hp == null) return;
        blood.fillAmount = hp.getCurrentHP() / hp.getMaxHP();
        hpText.text = System.Math.Round(hp.getCurrentHP()) + "/" + System.Math.Round(hp.getMaxHP());
    }

    private string AttackTypeToChinese(AttackType attackType)
    {
        switch (attackType)
        {
            case AttackType.Single:
                return "单体";               
            case AttackType.Aoe:
                return "范围";
            default:
                return "未知类型";
        }
    }
    private void UpdateBuff()
    {
        GameObject prefab = Resources.Load("Prefabs/UIPanel/BuffImage") as GameObject;
        //Instantiate(prefab, Vector3.zero, Quaternion.identity, buffStatus.transform);
        buffInfoList = target.GetComponent<BattleObject>().GetAllBuff();
        if(buffInfoList==null)
        {
            return;
        }
        if (buffImageList.Count > buffInfoList.Count)
        {
            for(int i=0;i< buffInfoList.Count;i++)
            {
                buffImageList[i].setBuff(buffInfoList[i]);
                buffImageList[i].GetComponent<CanvasGroup>().alpha = 1;
                buffImageList[i].GetComponent<CanvasGroup>().blocksRaycasts = true;
                buffImageList[i].GetComponent<CanvasGroup>().interactable = true;
            }
            for(int i = buffInfoList.Count; i < buffImageList.Count; i++)
            {
                buffImageList[i].GetComponent<CanvasGroup>().alpha = 0;
                buffImageList[i].GetComponent<CanvasGroup>().blocksRaycasts = false;
                buffImageList[i].GetComponent<CanvasGroup>().interactable = false;
            }
        }
        else
        {
            for (int i = 0; i < buffImageList.Count; i++)
            {
                buffImageList[i].setBuff(buffInfoList[i]);
                buffImageList[i].GetComponent<CanvasGroup>().alpha = 1;
                buffImageList[i].GetComponent<CanvasGroup>().blocksRaycasts = true;
                buffImageList[i].GetComponent<CanvasGroup>().interactable = true;
            }
            for (int i = buffImageList.Count; i < buffInfoList.Count; i++)
            {
                buffImageList.Add((Instantiate(prefab, Vector3.zero, Quaternion.identity, buffStatus.transform)as GameObject).GetComponent<BuffImage>());
                buffImageList[i].GetComponent<CanvasGroup>().alpha = 0;
                buffImageList[i].GetComponent<CanvasGroup>().blocksRaycasts = false;
                buffImageList[i].GetComponent<CanvasGroup>().interactable = false;
                buffImageList[i].setBuff(buffInfoList[i]);

            }
        }
    }

    public void SetTarget(GameObject target)
    {
        this.target = target;
    }

    public void SetTarget()
    {
        this.target = player;
    }
}